1937D - Pinball - CodeForces Solution


binary search implementation two pointers

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>  
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define pb push_back
#define tcs int T;cin>>T;while(T--)
#define rep(s,e,i) for (int i = s; i <= e; ++i)
#define revp(s,e,i) for (int i = s; i >= e; --i)
#define all(x) x.begin(), x.end()
#define out(x) cout<<((x)?"YES\n":"NO\n")
template <typename T> using ordered_set = 
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset = 
tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using min_heap = priority_queue<T,vector<T>,greater<T>>;
long long binexp(int x,int y,int mod){x=x%mod;int a=x,b=1;
while(y!=0){if(y&1){b=(1ll*b*a)%mod;}a=(1ll*a*a)%mod;y=(y>>1);}return b;}

signed main(){tcs{
int n;cin>>n;int psum1[n+2],psum2[n+2],p1[n+2],p2[n+2];
string s;cin>>s;rep(0,n+1,i){p1[i]=0;p2[i]=0;psum1[i]=0;psum2[i]=0;}
rep(1,n,i){psum1[i]=psum1[i-1];if(s[i-1]=='>'){++psum1[i];p1[psum1[i]]=p1[psum1[i]-1]+i;}}
revp(n,1,i){psum2[i]=psum2[i+1];if(s[i-1]=='<'){++psum2[i];p2[psum2[i]]=p2[psum2[i]-1]+i;}}
rep(1,n,i){int ans=0;
if(s[i-1]=='>'){
if(psum2[i+1]<=psum1[i-1]){ans=(p2[psum2[i+1]]-p1[psum1[i-1]]+p1[psum1[i-1]-psum2[i+1]])*2+n-i+1;}
else{ans=(p2[psum2[i+1]]-p1[psum1[i-1]]-p2[psum2[i+1]-psum1[i-1]-1])*2-i;}
}
else{
if(psum1[i-1]<=psum2[i+1]){ans=(p2[psum2[i+1]]-p2[psum2[i+1]-psum1[i-1]]-p1[psum1[i-1]])*2+i;}
else{ans=(p2[psum2[i+1]]-p1[psum1[i-1]]+p1[psum1[i-1]-psum2[i+1]-1])*2+n+i+1;}
}
cout<<ans<<" ";
}
cout<<endl;
}}


Comments

Submit
0 Comments
More Questions

454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine
1005A - Tanya and Stairways
1663F - In Every Generation
1108B - Divisors of Two Integers
1175A - From Hero to Zero
1141A - Game 23
1401B - Ternary Sequence
598A - Tricky Sum
519A - A and B and Chess
725B - Food on the Plane
154B - Colliders
127B - Canvas Frames
107B - Basketball Team
245A - System Administrator
698A - Vacations
1216B - Shooting
368B - Sereja and Suffixes
1665C - Tree Infection